package main 

import(	
	//"github.com/golang/protobuf/proto"
	"sanguo/node/common/aoi"
	"fmt"
	"math/rand"
)


const border int32 = 150

type User struct {
	aoi.AOIObjectBase
	id  int
}


func NewUser(id int) *User {
	u := &User{}
	u.Observers = map[aoi.AOIObject]bool{}
	u.id = id
	return u
}

/*func (this *User) BrocastToObserver(msg proto.Message,except map[aoi.AOIObject]bool) {
	this.Brocast(msg,except)
}*/

/*func (this *User) Send(msg proto.Message) {

}*/

func (this *User) OnAoiUpdate() {

}

func (this *User) GetAoiPosition() *aoi.Position {
	return &this.AoiPos	
}
	
func (this *User) SetAoiPosition(pos aoi.Position) {
	this.AoiPos = pos
}
	

func (this *User) GetGrid() *aoi.Grid {
	return this.Grid
}

func (this *User) SetGrid(grid *aoi.Grid) {
	this.Grid = grid
}
	
func (this *User) AddObserver(o aoi.AOIObject) {
	fmt.Println(this.id,"add observer",o.(*User).id)
	this.Observers[o] = true
}

func (this *User) RemoveObserver(o aoi.AOIObject) {
	fmt.Println(this.id,"remove observer",o.(*User).id)
	delete(this.Observers,o)
}

func (this *User) ForEachObserver(cb func(aoi.AOIObject)) {
	for o,_ := range(this.Observers) {
		cb(o)
	}
}

func (this *User) ClearObjserver() {
	this.Observers = map[aoi.AOIObject]bool{}
}


func (this *User) Move() {

	x := rand.Int31() % 10
	y := rand.Int31() % 10

	if rand.Int31() % 2 == 0 {
		x = x * -1
	}

	if rand.Int31() % 2 == 0 {
		y = y * -1
	}

	this.AoiPos.X += x
	this.AoiPos.Y += y

	if this.AoiPos.X < 1 {
		this.AoiPos.X = 0
	}

	if this.AoiPos.X >= border {
		this.AoiPos.X = border - 1
	}


	if this.AoiPos.Y < 1 {
		this.AoiPos.Y = 0
	}

	if this.AoiPos.Y >= border {
		this.AoiPos.Y = border - 1
	}

}

func main() {

	leftBottom := aoi.Position{
		X : 0,
		Y : 0,
	}

	rightTop  := aoi.Position{
		X : 150,
		Y : 150,
	}

	aoi_ := aoi.New(leftBottom,rightTop,5)

	user1 := NewUser(1)
	user1.AoiPos.X = 150
	user1.AoiPos.Y = 150
	aoi_.Add(user1)

	user2 := NewUser(2)
	user2.AoiPos.X = 76
	user2.AoiPos.Y = 76
	aoi_.Add(user2)


	user2.AoiPos.X = 120
	user2.AoiPos.Y = 120


	aoi_.Update(user2)




	//aoi_.Remove(user2)


}


/*
func main() {

	leftBottom := aoi.Position{
		X : 0,
		Y : 0,
	}

	rightTop  := aoi.Position{
		X : 100,
		Y : 110,
	}

	m := aoi.NewGridMgr(leftBottom,rightTop,10)

	g := m.GetGrids()

	fmt.Println(len(g),len(g[0]))


	grid := m.GetGridByPos(&aoi.Position{X:12,Y:12})

	fmt.Println(grid)

	//fmt.Println(m.GetNearBy(grid))

	grids := m.GetNearBy(grid)
	fmt.Println("--------------------------")
	for _,v := range(grids) {
		fmt.Println(v)
	}
}
*/